﻿// -----------------------------------------------------------------------
// <copyright file="Global_methods_user.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace IP_Package.Global_applications
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Global_methods_user : IPPackage.ImageOperation.Operation
    {
        /// <summary>
        /// Normalizations the specified gamma result.
        /// </summary>
        /// <param name="gammaRedBuffer">The gamma of red color for each pixels.</param>
        /// <param name="gammaGreenBuffer">The gamma of green color for each pixels.</param>
        /// <param name="gammaBlueBuffer">The gamma of blue color for each pixels.</param>
        /// <returns>
        /// new image after applying gamma
        /// </returns>
        public Bitmap Normalization(double[,] gammaRedBuffer, double[,] gammaGreenBuffer, double[,] gammaBlueBuffer)
        {
            double red_max = -1, green_max = -1, blue_max = -1, red_min = 256, green_min = 256, blue_min = 256;
            try
            {
                for (int i = 0; i < BufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < BufferOfImage.GetLength(0); j++)
                    {
                        if (gammaRedBuffer[j, i] > red_max)
                        {
                            red_max = gammaRedBuffer[j, i];
                        }

                        if (gammaRedBuffer[j, i] < red_min)
                        {
                            red_min = gammaRedBuffer[j, i];
                        }

                        if (gammaGreenBuffer[j, i] > green_max)
                        {
                            green_max = gammaGreenBuffer[j, i];
                        }

                        if (gammaGreenBuffer[j, i] < green_min)
                        {
                            green_min = gammaGreenBuffer[j, i];
                        }

                        if (gammaBlueBuffer[j, i] > blue_max)
                        {
                            blue_max = gammaBlueBuffer[j, i];
                        }

                        if (gammaBlueBuffer[j, i] < blue_min)
                        {
                            blue_min = gammaBlueBuffer[j, i];
                        }
                    }
                }

                this.NewBufferOfImage = new Color[gammaBlueBuffer.GetLength(0), gammaBlueBuffer.GetLength(1)];
                Bitmap new_Img = new Bitmap(gammaBlueBuffer.GetLength(0), gammaBlueBuffer.GetLength(1));
                for (int i = 0; i < gammaBlueBuffer.GetLength(1); i++)
                {
                    for (int j = 0; j < gammaBlueBuffer.GetLength(0); j++)
                    {
                        double blue;
                        double red;
                        double green;
                        if (blue_min != blue_max)
                        {
                            blue = ((gammaBlueBuffer[j, i] - blue_min) / (blue_max - blue_min)) * 255;
                        }
                        else
                        {
                            blue = 0;
                        }

                        if (red_max != red_min)
                        {
                            red = ((gammaRedBuffer[j, i] - red_min) / (red_max - red_min)) * 255;
                        }
                        else
                        {
                            red = 0;
                        }

                        if (green_max != green_min)
                        {
                            green = ((gammaGreenBuffer[j, i] - green_min) / (green_max - green_min)) * 255;
                        }
                        else
                        {
                            green = 0;
                        }

                        this.NewBufferOfImage[j, i] = Color.FromArgb((int)red, (int)green, (int)blue);
                        new_Img.SetPixel(j, i, Color.FromArgb((int)red, (int)green, (int)blue));
                    }
                }

                return new_Img;
            }
            catch (Exception ex)
            {
                IPPackage.Logger.LogException(ex);
                throw ex;
            }
        }

        /// <summary>
        /// Normalizations image.
        /// </summary>
        /// <param name="valueWouldNormalizeIt">The value would normalize it.</param>
        /// <returns>
        /// new image modified
        /// </returns>
        public Bitmap Normalization(double[,,] valueWouldNormalizeIt)
        {
            double red_max = -1, green_max = -1, blue_max = -1, red_min = 256, green_min = 256, blue_min = 256;
           try
            {
                for (int i = 0; i < valueWouldNormalizeIt.GetLength(1); i++)
                {
                    for (int j = 0; j < valueWouldNormalizeIt.GetLength(0); j++)
                    {
                        if (valueWouldNormalizeIt[j, i, 0] > red_max)
                        {
                            red_max = valueWouldNormalizeIt[j, i, 0];
                        }

                        if (valueWouldNormalizeIt[j, i, 0] < red_min)
                        {
                            red_min = valueWouldNormalizeIt[j, i, 0];
                        }

                        if (valueWouldNormalizeIt[j, i, 1] > green_max)
                        {
                            green_max = valueWouldNormalizeIt[j, i, 1];
                        }

                        if (valueWouldNormalizeIt[j, i, 1] < green_min)
                        {
                            green_min = valueWouldNormalizeIt[j, i, 1];
                        }

                        if (valueWouldNormalizeIt[j, i, 2] > blue_max)
                        {
                            blue_max = valueWouldNormalizeIt[j, i, 2];
                        }

                        if (valueWouldNormalizeIt[j, i, 2] < blue_min)
                        {
                            blue_min = valueWouldNormalizeIt[j, i, 2];
                        }
                    }
                }

                this.NewBufferOfImage = new Color[valueWouldNormalizeIt.GetLength(0), valueWouldNormalizeIt.GetLength(1)];
                Bitmap new_Img = new Bitmap(valueWouldNormalizeIt.GetLength(0), valueWouldNormalizeIt.GetLength(1));
                for (int i = 0; i < valueWouldNormalizeIt.GetLength(1); i++)
                {
                    for (int j = 0; j < valueWouldNormalizeIt.GetLength(0); j++)
                    {
                        double blue;
                        double red;
                        double green;
                        if (blue_max != blue_min)
                        {
                            blue = ((valueWouldNormalizeIt[j, i, 2] - blue_min) / (blue_max - blue_min)) * 255;
                        }
                        else
                        {
                            blue = 0;
                        }

                        if (red_max != red_min)
                        {
                            red = ((valueWouldNormalizeIt[j, i, 0] - red_min) / (red_max - red_min)) * 255;
                        }
                        else
                        {
                            red = 0;
                        }

                        if (green_max != green_min)
                        {
                            green = ((valueWouldNormalizeIt[j, i, 1] - green_min) / (green_max - green_min)) * 255;
                        }
                        else
                        {
                            green = 0;
                        }

                        this.NewBufferOfImage[j, i] = Color.FromArgb((int)red, (int)green, (int)blue);
                        new_Img.SetPixel(j, i, Color.FromArgb((int)red, (int)green, (int)blue));
                    }
                }

                return new_Img;
           }
            catch (Exception ex)
            {
                IPPackage.Logger.LogException(ex);
                throw ex;
            }
        }    
     
        /// <summary>
        /// Normalizations the specified color.
        /// </summary>
        /// <param name="color">The color need normalize it.</param>
        /// <param name="valueWouldNormalizeIt">The value would normalize it.</param>
        /// <returns>
        /// the result of normalization
        /// </returns>
        public Bitmap Normalization(string color, double[,] valueWouldNormalizeIt)
        {
            double max = -1, min = 256;
            try
            {
                for (int i = 0; i < valueWouldNormalizeIt.GetLength(1); i++)
                {
                    for (int j = 0; j < valueWouldNormalizeIt.GetLength(0); j++)
                    {
                        if (valueWouldNormalizeIt[j, i] > max)
                        {
                            max = valueWouldNormalizeIt[j, i];
                        }

                        if (valueWouldNormalizeIt[j, i] < min)
                        {
                            min = valueWouldNormalizeIt[j, i];
                        }
                    }
                }

                Bitmap newImg = new Bitmap(valueWouldNormalizeIt.GetLength(0), valueWouldNormalizeIt.GetLength(1));
                if (min != max)
                {
                    for (int i = 0; i < valueWouldNormalizeIt.GetLength(1); i++)
                    {
                        for (int j = 0; j < valueWouldNormalizeIt.GetLength(0); j++)
                        {
                            if (color == "red" || color == "Red")
                            {
                                double blue = 0;
                                double red = ((valueWouldNormalizeIt[j, i] - min) / (max - min)) * 255;
                                double green = 0;
                                newImg.SetPixel(j, i, Color.FromArgb((int)red, (int)green, (int)blue));
                            }
                            else if (color == "green" || color == "Green")
                            {
                                double blue = 0;
                                double red = 0;
                                double green = ((valueWouldNormalizeIt[j, i] - min) / (max - min)) * 255;
                                newImg.SetPixel(j, i, Color.FromArgb((int)red, (int)green, (int)blue));
                            }
                            else
                            {
                                double blue = ((valueWouldNormalizeIt[j, i] - min) / (max - min)) * 255;
                                double red = 0;
                                double green = 0;
                                newImg.SetPixel(j, i, Color.FromArgb((int)red, (int)green, (int)blue));
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < valueWouldNormalizeIt.GetLength(1); i++)
                    {
                        for (int j = 0; j < valueWouldNormalizeIt.GetLength(0); j++)
                        {
                            newImg.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                        }
                    }
                }

                return newImg;
            }
            catch (Exception ex)
            {
               IPPackage.Logger.LogException(ex);
                throw ex;
            }
        }
    }
}
